package com.radar.example;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.radar.common.ProductType;
import com.radar.exception.RadarCaclError;
import com.radar.exception.RadarDecodeError;
import com.radar.exception.RadarVisualizeError;
import com.radar.io.CinradRadar;
import com.radar.visualize.PolarMap;
import com.radar.visualize.RasterMap;
import com.radar.visualize.VCSMap;
import org.junit.Test;
import org.meteoinfo.global.PointD;
import ucar.ma2.InvalidRangeException;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class VisualizeExample {

    static Log log = LogFactory.get();

    @Test
    public void drawPolar() throws RadarDecodeError, RadarCaclError, RadarVisualizeError, IOException {
        long time = System.currentTimeMillis();
        String file = "/Users/xx/data/micaps/SINGLERADAR/ARCHIVES/PRE_QC/Z9092/Z_RADR_I_Z9092_20190729220800_O_DOR_SA_CAP.bin.bz2";
        CinradRadar cinradRadar = new CinradRadar();
        cinradRadar.parse(file);
        PolarMap polarMap = new PolarMap(cinradRadar.radar);
        for (int i = 0; i < cinradRadar.radar.getElevation().length; i++) {
            // 基本反射率 i ==1 i == 3没有数据
            if (i == 1 || i == 3) {
                continue;
            }
            PolarMap.PolarInfo ref = PolarMap.buildPolarInfo(i, ProductType.REF)
                    // 背景颜色
                    .backColor(Color.black)
                    // 是否显示右边详细信息 以及图例
                    .simpleDetail(true)
                    // 是否画范围 不需要的话可以不调用该函数
                    .rings(new int[]{50, 100, 150, 200, 230, 400, 460});
            BufferedImage bufferedImage = polarMap.simplePolar(ref);
            ImageIO.write(bufferedImage, "png", new File("/Users/xx/DeskTop/polar_" + i + ".png"));
        }
        cinradRadar.dispose();
        log.info("time : {}", System.currentTimeMillis() - time);
    }

    @Test
    public void drawRaster() throws RadarDecodeError, RadarCaclError, RadarVisualizeError, IOException, InvalidRangeException {
        long time = System.currentTimeMillis();
        String file = "/Users/xx/data/micaps/SINGLERADAR/ARCHIVES/PRE_QC/Z9836/Z_RADR_I_Z9836_20190730001000_O_DOR_CA_CAP.bin.bz2";
        CinradRadar cinradRadar = new CinradRadar();
        cinradRadar.parse(file);
        RasterMap rasterMap = new RasterMap(cinradRadar.radar);
        RasterMap.RasterInfo rasterInfo = RasterMap.buildRasterInfo(ProductType.REF)
                // 背景颜色
                .backColor(Color.black)
                // 是否添加地理信息
                .drawCity(true)
                // 是否显示详细信息以及图例
                .simpleDetail(true)
                // 是否画范围 半径 km
                .rings(new int[]{50, 100, 150, 200});
        for (int i = 0; i < cinradRadar.radar.getElevation().length; i++) {
            // 基本反射率 i ==1 i == 3没有数据
            if (i == 1 || i == 3) {
                continue;
            }
            BufferedImage bufferedImage = rasterMap.simplePPI(rasterInfo, i);
            ImageIO.write(bufferedImage, "png", new File("/Users/xx/DeskTop/ppi_" + i + ".png"));
        }
        BufferedImage bufferedImage = rasterMap.simpleCR(rasterInfo);
        ImageIO.write(bufferedImage, "png", new File("/Users/xx/DeskTop/CR.png"));
        cinradRadar.dispose();
        log.info("time : {}", System.currentTimeMillis() - time);
    }

    public void drawVCS() throws RadarDecodeError, RadarVisualizeError, RadarCaclError, IOException {
        long time = System.currentTimeMillis();
        String file = "/Users/xx/data/micaps/SINGLERADAR/ARCHIVES/PRE_QC/Z9818/Z_RADR_I_Z9818_20190730000100_O_DOR_SC_CAP.bin.bz2";
        CinradRadar cinradRadar = new CinradRadar();
        cinradRadar.parse(file);
        List<PointD> list = new ArrayList<PointD>();
        list.add(new PointD(106.78, 32.36));
        list.add(new PointD(108.92, 31.53));
        VCSMap vcsMap = new VCSMap(cinradRadar.radar);
        VCSMap.VCSInfo vcsInfo = vcsMap.buildRasterInfo(ProductType.REF)
                .altNum(100)
                .pointNum(500)
                .maxAlt(22);
        BufferedImage bufferedImage = vcsMap.simpleVCS(vcsInfo, list);
        ImageIO.write(bufferedImage, "png", new File("/Users/xx/DeskTop/VCS.png"));
        cinradRadar.dispose();
        log.info("time : {}", System.currentTimeMillis() - time);
    }

}
